package cn.xuexiyuan.springdatamongodemo.user.controller;

import cn.xuexiyuan.springdatamongodemo.user.entity.User;
import cn.xuexiyuan.springdatamongodemo.user.service.UserService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.xuexiyuan.springdatamongodemo.common.Response;
import java.util.List;

/**
 * @Description:
 * @Author 左龙龙
 * @Date 20-1-14
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/find")
    Response find(@RequestBody JSONObject params){
        try{
            String name = params.getString("name");
            List<User> users = userService.find(name);

            return Response.buildSuccess(users);
        }catch (Exception e){
            log.error("查询用户异常", e);
            return Response.buildFail("未知的错误");
        }
    }
}
